'''
对该目录的所有图片进行循环，取第i个图片，依次用后面的图j比对i，如果相似，删除j
如果j的大小小于1k，删除j
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.metrics import structural_similarity as ssmi
import shutil
from tqdm import tqdm
from PIL import Image
from numpy import average, dot, linalg


resize_shape = [48,48]
path = r'/media/liyan/3b75ab20-92d3-4be6-81c2-4e1798e2fe16/我的数据集/图像分类数据集/总/印章(3573)/'

def get_brightness(img_name:str):
    '''
    获取图片亮度
    '''
    rgb_image = cv2.imread(path+img_name)
    # image = cv2.cvtColor(rgb_image,cv2.COLOR_BGR2RGB)
    brightness_mean = cv2.mean(rgb_image)
    return (round(brightness_mean[0]),
            round(brightness_mean[1]),
            round(brightness_mean[2]),
            round(brightness_mean[3])),rgb_image

def move_file(filename1,filename2):
    shutil.move(filename1,filename2)

def delete(filename1):
    os.remove(filename1)

def get_thum(image, size=resize_shape, greyscale=False):
    '''
    对图片进行统一化处理
    '''
    # 利用image对图像大小重新设置, Image.ANTIALIAS为高质量的
    image = cv2.resize(image,size)
    if greyscale:
        # 将图片转换为L模式，其为灰度图，其每个像素用8个bit表示
        # image = image.convert('L')
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return image


def image_similarity_vectors_via_numpy(image1, image2):
    '''
    计算图片的余弦距离
    '''
    image1 = get_thum(image1)
    image2 = get_thum(image2)
    images = [image1, image2]
    vectors = []
    norms = []
    for image in images:
        vector = []
        for pixel_tuple in image.getdata():
            vector.append(average(pixel_tuple))
        vectors.append(vector)
        # linalg=linear（线性）+algebra（代数），norm则表示范数
        # 求图片的范数
        norms.append(linalg.norm(vector, 2))
    a, b = vectors
    a_norm, b_norm = norms
    # dot返回的是点积，对二维数组（矩阵）进行计算
    res = dot(a / a_norm, b / b_norm)
    return res

if __name__ == '__main__':
    del_list = []  # 需要删除的图片列表
    img_files = [os.path.join(path,file) for file in os.listdir(path) if (file.endswith('.jpg'))]
    for currIndex, filename in enumerate(img_files):
        new_cur = 0
        for i in tqdm(range(len(img_files)-currIndex-1),ncols=150): #选择一张图，逐图片与其他图片比较
            currIndex1 = new_cur
            if currIndex1 >= len(img_files) - currIndex - 1:
                break
            else:
                size = os.path.getsize(img_files[currIndex1 + currIndex + 1])       #返回文件大小
                brit, img = get_brightness(path+filename)
                if size <= 2048:         #小于1kB的会被删除
                    del_list.append(img_files.pop(currIndex1 + currIndex + 1))
                    print(f'\n删除了一个小文件')
                elif (sum(brit[:-1]) / len(brit[:-1]) >= 210):
                    del_list.append(img_files.pop(currIndex1 + currIndex + 1))
                    print(f'\n删除了一个亮文件')
                else:
                    img = cv2.imread(img_files[currIndex])          #外循环，第currIndex张图
                    img1 = cv2.imread(img_files[currIndex1 + currIndex + 1])        #内循环

                    img = cv2.resize(img, resize_shape, interpolation=cv2.INTER_CUBIC)      #转换成46大小，便于比对
                    img1 = cv2.resize(img1, resize_shape, interpolation=cv2.INTER_CUBIC)    #转换成46大小，便于比对

                    ssim = ssmi(im1=img, im2=img1,win_size=7,multichannel=True,gaussian_weights=False,)       #SSMI相似度

                    # ssim = image_similarity_vectors_via_numpy(img, img1)        #余弦相似度

                    if ssim > 0.7:      #ssim大于多少会被删除
                        print(f'\n 删除了一个相似文件， {img_files[currIndex1+currIndex+1]}')
                        del_list.append(img_files.pop(currIndex1 + currIndex + 1))
                        new_cur = currIndex1
                    else:
                        new_cur = currIndex1 + 1
    for image in del_list:
        delete(image)           #删除
